package groups.common.gui;
import groups.common.DBDumper;
import groups.common.dao.IUserDao;
import groups.common.dao.JDBCUserDao;

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import javax.swing.WindowConstants;
import org.jdesktop.application.Application;
import javax.swing.SwingUtilities;
import java.sql.Connection;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class UserCreate extends javax.swing.JFrame{
	private JPanel topPanelEast;
	private JRadioButton type2Button;
	private JRadioButton type4Button;
	private JRadioButton type3Button;
	private JRadioButton type1Button;
	private JTextPane passkey1;
	private JTextField nameInput;
	private ButtonGroup typeGroupButton;
	private JTextPane name;
	private JButton cancel;
	private JButton reset;
	private JButton confirm;
	private JPanel topPanelWest;
	private JPasswordField passkeyInput;
	private JTextPane type;
	private JPanel topPaneCenter;
	private Connection con;
	/**
	* Auto-generated main method to display this JFrame
	*/
		
	public UserCreate(Connection con) {
		super("Criar Usu\u00E1rio");
		this.con = con;
		frameInit();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				topPanelEast = new JPanel();
				getContentPane().add(topPanelEast, BorderLayout.EAST);
				topPanelEast.setPreferredSize(new java.awt.Dimension(120, 270));
				topPanelEast.setName("topPanelEast");
				{
					confirm = new JButton();
					topPanelEast.add(confirm);
					confirm.setName("confirm");
					confirm.setPreferredSize(new java.awt.Dimension(105, 25));
					confirm.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							System.out.println("confirm.actionPerformed, event="+evt);
							actionConfirm();
						}
					});
				}
				{
					reset = new JButton();
					topPanelEast.add(reset);
					reset.setName("reset");
					reset.setPreferredSize(new java.awt.Dimension(105, 25));
				}
				{
					cancel = new JButton();
					topPanelEast.add(cancel);
					cancel.setName("cancel");
					cancel.setPreferredSize(new java.awt.Dimension(105
							, 25));
					cancel.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							System.out.println("cancel.actionPerformed, event="+evt);
							dispose();
						}
					});
				}
			}
			{
				topPaneCenter = new JPanel();
				topPaneCenter.setName("topPaneCenter");
				getContentPane().add(topPaneCenter, BorderLayout.CENTER);
				{
					nameInput = new JTextField();
					topPaneCenter.add(nameInput);
					nameInput.setPreferredSize(new java.awt.Dimension(220, 20));
				}
				{
					passkeyInput = new JPasswordField();
					topPaneCenter.add(passkeyInput);
					passkeyInput.setPreferredSize(new java.awt.Dimension(220, 20));
				}
				{
					type1Button = new JRadioButton();
					topPaneCenter.add(type1Button);
					type1Button.setName("type1Button");
					type1Button.setEnabled(true);
					getTypeGroupButton().add(type1Button);
				}
				{
					type2Button = new JRadioButton();
					topPaneCenter.add(type2Button);
					type2Button.setName("type2Button");
					getTypeGroupButton().add(type2Button);
				}
				{
					type3Button = new JRadioButton();
					topPaneCenter.add(type3Button);
					type3Button.setName("type3Button");
					getTypeGroupButton().add(type3Button);
				}
				{
					type4Button = new JRadioButton();
					topPaneCenter.add(type4Button);
					type4Button.setName("type4Button");
					getTypeGroupButton().add(type4Button);
				}
			}
			{
				topPanelWest = new JPanel();
				getContentPane().add(topPanelWest, BorderLayout.WEST);
				topPanelWest.setPreferredSize(new java.awt.Dimension(53, 158));
				topPanelWest.setName("topPanelWest");
				{
					name = new JTextPane();
					topPanelWest.add(name);
					name.setName("name");
					name.setPreferredSize(new java.awt.Dimension(50, 20));
				}
				{
					passkey1 = new JTextPane();
					topPanelWest.add(passkey1);
					passkey1.setName("passkey1");
					passkey1.setPreferredSize(new java.awt.Dimension(50, 20));
				}
				{
					type = new JTextPane();
					topPanelWest.add(type);
					type.setName("type");
					type.setPreferredSize(new java.awt.Dimension(50, 20));
				}
			}
			pack();
			this.setSize(403, 187);
			Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(getContentPane());
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	public JTextField getNameInput() {
		return nameInput;
	}
	
	public JTextField getPasskeyInput() {
		return passkeyInput;
	}
	
	private ButtonGroup getTypeGroupButton() {
		if(typeGroupButton == null) {
			typeGroupButton = new ButtonGroup();
		}
		return typeGroupButton;
	}
	
	private int getType(){
		if (typeGroupButton.getSelection() == type2Button){
			return 2;
		}
		else if (typeGroupButton.getSelection() == type3Button){
			return 3;
		}
		else if (typeGroupButton.getSelection() == type4Button){
			return 4;
		}
		else return 1;
	}
	
	private void actionConfirm(){
		IUserDao user = new JDBCUserDao(con);
		String passkey = new String (passkeyInput.getPassword());
		user.create(nameInput.getText(), passkey, getType());
	}
}
